package lol.clann.minecraft.springboot.plugin.bukkit.command.resolver.factory;

import lol.clann.minecraft.springboot.api.bukkit.command.resolver.CommandResolver;
import lol.clann.minecraft.springboot.api.bukkit.command.resolver.CommandResolverFactory;
import org.bukkit.Bukkit;
import org.bukkit.command.CommandException;
import org.bukkit.entity.Player;
import org.springframework.stereotype.Component;

/**
 *
 * @author pyz
 * @date 2019/5/6 9:29 PM
 */
@Component
public class PlayerCommandResolverFactory implements CommandResolverFactory {
    @Override
    public boolean canResolve(Class<?> type) {
        return Player.class.isAssignableFrom(type);
    }

    @Override
    public CommandResolver getResolver(Class<?> clazz) {
        return (sender, arg, type) -> {
            Player player = Bukkit.getServer().getPlayer(arg);
            if (player.getName().equals(arg)) {
                return player;
            }
            throw new CommandException("玩家不存在或者不在线:" + arg);
        };
    }
}
